#include "offlinemessagemodel.hpp"
#include "db.h"
#include <string>
#include <vector>

using namespace std;

void OfflineMessageModel::insert(int userid, string msg)
{
    MySQL mysql;
    char sql[256] = {0};
    sprintf(sql, "insert into offlinemessage Values(%d, '%s')", userid, msg.c_str());
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}
void OfflineMessageModel::remove(int userid)
{
    MySQL mysql;
    char sql[256] = {0};
    sprintf(sql, "delete from offlinemessage where userid=%d", userid);
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}
vector<string> OfflineMessageModel::query(int userid)
{
    vector<string> vec;
    MySQL mysql;
    char sql[256] = {0};
    sprintf(sql, "select message from offlinemessage where userid=%d", userid);
    if(mysql.connect()) {
        MYSQL_RES* res = mysql.query(sql);
        if(res != nullptr) {
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr) {
                vec.push_back(row[0]);
            }
            mysql_free_result(res);
        }
    }
    return vec;
}